package 双指针.三数之和;

import java.util.*;

public class ThreeNum {
    public static void main(String[] args) {
        int[] arr ={-1,0,1,2,-1,-4};
        Solution so = new Solution();
        List<List<Integer>> rel = so.threeSum(arr);
        System.out.println(rel);
    }
}
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);//排序
        Set<List<Integer>> rel = new HashSet<>();
        for(int top = 0; top < nums.length-2;top++){
            //去重
            /*if(top >=1 && nums[top] == nums[top-1]){
                continue;
            }*/
            int left = top+1,right = nums.length-1;
            int target = -nums[top];
            while(left < right){
                if(nums[left]+nums[right] > target){
                    right--;
                }else if(nums[left]+nums[right] < target){
                    left++;
                }else{
                    List<Integer> result =Arrays.asList(nums[top],nums[left],nums[right]);
                    rel.add(result);

                    right--;
                    left++;

                    //去重
                    /*while(left < right && nums[left] == nums[left-1]){
                        left++;
                    }
                    while(left < right && nums[right] == nums[right+1]){
                        right--;
                    }*/
                }
            }
        }
        return new ArrayList<>(rel);
    }
}